import React, {Component} from 'react';
import './index.less';
import Header from '../components/Header';
import SearchModal from '../components/SearchModal';
import getMainTableData from './functions/getMainTableData';
import MainTable from '../components/MainTable';
import saveOpinionData from './functions/saveOpinionData';
import updateButtonStatus from './functions/updateButtonStatus';
import StopModal from '../components/StopModal';
import OpinionAsk from '../../../../public/components/opinionAsk';
import stopConConfirm from './functions/stopConConfirm';
import AlertModal from '../components/AlertModal';
import StopAgreeModal from "../components/StopAgreeModal";
import stopAgree from './functions/stopAgree';
import processTemplate from './functions/processTemplateData';
import { getAppPageConfig } from "src/hrpub/common/utils/utils";

// @handleHash('20198162232323', '/ifr?page=20198162232323&c=60110313&p=60110313p&ar=0001Z700APPN60110313')
class ContOpinion extends Component {
    constructor(props) {
        super(props);
        if (props.use) {
            props.use.form('contopinion', 'ctrtoperatedlg')
            props.use.table('contopinionadd')
            props.use.editTable('contopiniongrid', 'agreementgrid')
            props.use.search('search')
        }
        this.config = getAppPageConfig();
        this.state = {
            json: {},
            orgVal: {},
            isEdit: false,
            searchModalValue: {},
            stopModalVisible: false,
            addNewModalVisible: false,
            stopAgreeVisible: false,
            stopPkOpinions: ''
        };
        props.MultiInit.getMultiLang({
            moduleId: 'cm6011',
            domainName: 'hrcm',
            callback: (json, status, inlt) => {
                if (status) {
                    this.setState({json, inlt}, () => {
                        props.createUIDom(this.config, (data) => {
                            this.setState({
                                buttons: data.button || [],
                                context: data.context || {},
                                queryOid: data.template.search.oid,
                                pageid: data.template.pageid
                            });
                            data.template = processTemplate.call(this, data.template);
                            props.meta.setMeta(data && data.template ? data.template : {});
                            props.button.setButtons(data && data.button ? data.button : {});
                            updateButtonStatus.call(this, this.state.orgVal);
                        });
                    }); //存json和inlt到页面state中并刷新页面
                }
            }
        });
        this.updateState = this.updateState.bind(this);
        this.getMainTableData = this.getMainTableData.bind(this);
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.saveOpinion = this.saveOpinion.bind(this);
        this.confirmStop = this.confirmStop.bind(this);
        this.showOpinionAsk = this.showOpinionAsk.bind(this);
        this.stopAgree = this.stopAgree.bind(this);
    }

    componentDidMount() {
        // if (window.location.href.match('localhost:3006')) 
        // window.location.hash = `#/ifr?page=20198162232323`;
    }

    updateOrgValue(value) {
        this.setState(
            {
                orgVal: value
            },
            () => {
                if (!value.refpk) {
                    this.props.editTable.setTableData('contopiniongrid', {rows: []}, false);
                    updateButtonStatus.call(this, this.state.orgVal);
                } else {
                    this.getMainTableData();
                }
            }
        );
    }

    updateState(data, callback = () => {
    }) {
        this.setState(data, callback);
    }

    getMainTableData(isRefresh) {
        getMainTableData.call(this, isRefresh);
    }

    saveOpinion(callback) {
        saveOpinionData.call(this, callback);
    }

    confirmStop() {
        stopConConfirm.call(this);
    }

    showOpinionAsk() {
        this.opinionAskNode.open();
    }

    stopAgree() {
        stopAgree.call(this);
    }

    render() {
        const {orgVal, isEdit, stopModalVisible, stopAgreeVisible, stopPkOpinions, queryOid, json} = this.state;
        return (
            <div className="entry-staff-query nc-bill-card">
                <Header
                    {...this.props}
                    json={json}
                    orgVal={orgVal}
                    isEdit={isEdit}
                    editTable={this.props.editTable}
                    getMainTableData={this.getMainTableData}
                    saveOpinion={this.saveOpinion}
                    updateState={this.updateState}
                    updateOrgValue={this.updateOrgValue}
                    showOpinionAsk={this.showOpinionAsk}
                />
                <div className="entry-staff-query-content">
                    <MainTable {...this.props} orgVal={orgVal} isEdit={isEdit} json={json}/>
                </div>

                <SearchModal
                    {...this.props}
                    queryOid={queryOid}
                    updateState={this.updateState}
                    getMainTableData={this.getMainTableData}
                />

                <StopModal
                    {...this.props}
                    json={json}
                    visible={stopModalVisible}
                    stopPkOpinions={stopPkOpinions}
                    beSure={this.confirmStop}
                    onClose={() => {
                        this.setState({
                            stopModalVisible: false
                        });
                    }}
                />

                <OpinionAsk
                    {...this.props}
                    orgVal={orgVal}
                    confirmAction={this.getMainTableData}
                    confirmAskAction={this.getMainTableData}
                    ref={node => this.opinionAskNode = node}
                />

                <StopAgreeModal
                    {...this.props}
                    json={json}
                    visible={stopAgreeVisible}
                    beSure={this.stopAgree}
                    onClose={() => {
                        this.setState({
                            stopModalVisible: false,
                            stopAgreeVisible: false
                        });
                    }}
                />

                <AlertModal ref={(node) => (this.alertModalNode = node)} json={json}/>
            </div>
        );
    }
}

export default ContOpinion;
